package com.wolfcode.goods.service.impl;

import com.wolfcode.common.utils.file.FileUploadUtils;
import com.wolfcode.goods.domain.Goods;
import com.wolfcode.goods.domain.vo.GoodsVo;
import com.wolfcode.goods.mapper.GoodsMapper;
import com.wolfcode.goods.service.IGoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * 物品信息Service业务层处理
 * 
 * @author wolfcode
 * @date 2022-12-10
 */
@Service
public class GoodsServiceImpl implements IGoodsService 
{
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 查询物品信息
     * 
     * @param id 物品信息主键
     * @return 物品信息
     */
    @Override
    public Goods selectGoodsById(String id)
    {
        return goodsMapper.selectGoodsById(id);
    }

    /**
     * 查询物品信息列表
     * 
     * @param goodsVo 物品信息
     * @return 物品信息
     */
    @Override
    public List<Goods> selectGoodsList(GoodsVo goodsVo)
    {
        Goods goods = new Goods();
        if (goodsVo.getGoodsName() != null) {
            goods.setGoodsName(goodsVo.getGoodsName());
        };
        if (goodsVo.getBusiPath() != null) {
            List<String> busiPaths = goodsVo.getBusiPath();
            String busiPath = busiPaths.get(busiPaths.size() - 1);
            goods.setCategoryId(busiPath);
        }
        //todo 货物仓库表中没有数量查不出数据
        //todo 模糊查询中四个数据任一条没有则查不出数据,根据数量模糊查询失效
        return goodsMapper.selectGoodsList(goods);
    }

    /**
     * 新增物品信息
     * 
     * @param goodsVo 物品信息
     * @return 结果
     */
    @Override
    public int insertGoods(GoodsVo goodsVo) throws IOException, InvalidKeyException,NoSuchAlgorithmException{
        //参数校验
        Assert.notNull(goodsVo.getGoodsName(),"名称不能为空");
        Assert.notNull(goodsVo.getGoodsCover(),"图片不能为空");
        Assert.notNull(goodsVo.getBusiPath(),"分类不能为空");
        String url = FileUploadUtils.upload(goodsVo.getGoodsCover());
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsVo,goods);
        goods.setGoodsCover(url);
        return goodsMapper.insertGoods(goods);
    }

    /**
     * 修改物品信息
     * 
     * @param goodsVo 物品信息
     * @return 结果
     */
    @Override
    public int updateGoods(GoodsVo goodsVo)
    {
        //参数校验
        Assert.notNull(goodsVo, "参数异常");
        Assert.notNull(goodsVo.getGoodsName(),"名称不能为空");
        Assert.notNull(goodsVo.getGoodsCover(),"图片不能为空");
        //todo 联调时打开
        //Assert.notNull(goodsVo.getBusiPath(),"分类不能为空");
        String busiPath = null;
        if (goodsVo.getBusiPath() != null) {
            List<String> busiPaths = goodsVo.getBusiPath();
            busiPath = busiPaths.get(busiPaths.size() - 1);
        }
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsVo,goods);
        goods.setCategoryId(busiPath);
        return goodsMapper.updateGoods(goods);
    }

    /**
     * 批量删除物品信息
     * 
     * @param ids 需要删除的物品信息主键
     * @return 结果
     */
    @Override
    public int deleteGoodsByIds(Long[] ids)
    {
        goodsMapper.deleteAmount(ids);
        return goodsMapper.deleteGoodsByIds(ids);
    }

    /**
     * 删除物品信息信息
     * 
     * @param id 物品信息主键
     * @return 结果
     */
    @Override
    public int deleteGoodsById(Long id)
    {
        return goodsMapper.deleteGoodsById(id);
    }
}
